package com.woniu.controller;

import com.woniu.entity.Menu;
import com.woniu.entity.User;
import com.woniu.entity.response.ResponseEntity;
import com.woniu.security.JwtUtil;
import com.woniu.service.IMenuService;
import com.woniu.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class IndexController {
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IUserService userService;
    //获取用户权限菜单
    @GetMapping("/indexuser")
    public ResponseEntity<List<Menu>> listUserMenus(@RequestHeader String jwt) {   //获取请求头
        if (jwtUtil.verifyJWT(jwt)) {
            int id = (int) jwtUtil.getInfo(jwt, "id");
            User user = userService.getById(id);
            return ResponseEntity.success(menuService.getByRoleId(user.getRole().getRoleId()));
        } else {
            return ResponseEntity.fail("502","no token");
        }
    }
    /**
     * 获取首页用户信息
     * */
    @GetMapping("indexusertoken")
    public ResponseEntity<String> account(@RequestHeader String jwt){

        if (jwtUtil.verifyJWT(jwt)) {
            String account = jwtUtil.getInfo(jwt, "account")+"";
            return ResponseEntity.success(account);
        } else {
            return ResponseEntity.fail("502","no token");
        }
    }
}
